//cWindow.cpp
#include "cWindow.h"

//Used by signal handlers to identify a particular instance of the class
//given its GTKWin, by iterating through this
std::list<Window*> Window::Instances;

/* Default constructor.
 */
Window::Window()
{
	this->_Create("", 0, 0);
}

/* Constructor with title.
 * Inputs:
 * -Title: Window title.
 */
Window::Window(std::string Title)
{
	this->_Create(Title, 0, 0);
}

/* Constructor with size.
 * Inputs:
 * -Width, Height: Dimensions of window.
 */
Window::Window(u32 Width, u32 Height)
{
	this->_Create("", Width, Height);
}

/* Constructor with title and size.
 * Inputs:
 * -Title: Window title.
 * -Width, Height: Dimensions of box.
 */
Window::Window(std::string Title, u32 Width, u32 Height)
{
	this->_Create(Title, Width, Height);
}

/* PROTECTED: Internal constructor routine.
 * Inputs:
 * -Title: Window title.
 * -Width, Height: Dimensions of window. Zero for default.
 */
void Window::_Create(std::string Title, u32 Width, u32 Height)
{
	//Create the window and connect signals.
	this->GTKWin = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
	if(Width && Height) this->SetSize(Width, Height);
	this->SetTitle(Title);
	
	Window::Instances.push_front(this);
}

/* Destructor.
 */
Window::~Window()
{
	Window::Instances.remove(this);
}

/* Sets the window's default size.
 * Inputs:
 * -Width, Height: Window size.
 */
void Window::SetSize(u32 Width, u32 Height)
{
	gtk_window_set_default_size(this->GTKWin, Width, Height);
}

/* Sets the window's title.
 * Inputs:
 * -Title: Window title.
 */
void Window::SetTitle(std::string Title)
{
	this->Title = Title;
	gtk_window_set_title(this->GTKWin, Title.c_str());
}

/* Retrieves the window's width.
 * Returns: obvious.
 */
u32 Window::GetWidth()
{
	return GTK_WIDGET(this->GTKWin)->allocation.width;
}

/* Retrieves the window's height.
 * Returns: obvious.
 */
u32 Window::GetHeight()
{
	return GTK_WIDGET(this->GTKWin)->allocation.height;
}

/* Retrieves the window's title.
 * Returns: obvious.
 */
std::string Window::GetTitle()
{
	return this->Title;
}

/* Retrieves the GTK window handle.
 * Returns: obvious.
 */
GtkWindow* Window::GetGTKWin()
{
	return this->GTKWin;
}

/* FRIEND: Retrieve a Window given its GTKWin.
 * Inputs:
 * -Win: GTKWin to look up.
 * Returns: Pointer to Window on success, NULL on failure.
 */
Window* LookupWindow(GtkWindow *Win)
{
	for(std::list<Window*>::iterator i = Window::Instances.begin();
		i != Window::Instances.end(); i++)
	{
		if(*i && (*i)->GTKWin == Win) return *i;
	}
	return NULL;
}
